#!/usr/bin/node

const EventEmitter = require('events').EventEmitter;

function MusicPlayer(track) {
  this.track = track;
  this.playing = false;
  for(var methodName in EventEmitter.prototype) {
    //console.log(methodName);
     this[methodName] = EventEmitter.prototype[methodName];      
    }
}

MusicPlayer.prototype = {
  toString: () => {
    if(this.playing) {
      return 'Now Playing: ' + this.track;      } else {
      return 'Stopped';          
    }  
  }
};

var musicPlayer = new MusicPlayer('storm');

musicPlayer.on('play', () => {
  this.playing = true;
  console.log('\n', this.toString());
});

musicPlayer.on('stop', () => {
  this.playing = false;
  console.log('\n', this.toString());

});

//捕获异常的两种方式
musicPlayer.on('error',function(err){
  console.log(err);
  process.exit(1);
})
/*
 * 捕获未处理的异常
process.on('uncaughtException', (err) => {
    console.error(err);
    process.exit(1);

});
*/
musicPlayer.emit('play');

setTimeout(() => {
    musicPlayer.emit('stop');
}, 2000);

//编写一个异常
setTimeout(() => {
    musicPlayer.emit('error', new Error('WRONG!'));
}, 3000);

